/*
 * @Author: mayijun
 * @Date: 2022-05-10 17:15:25
 * @LastEditors: mayijun
 * @LastEditTime: 2022-06-30 10:51:52
 */
import { defineStore } from 'pinia';
import { http, URL } from '@/utils/http';
export interface ISelectPermission {
  resourceName: string;
  resourceCode: string;
  resourcePath: string;
  resourceLink: string;
  resourceIcon: string;
  default: boolean;
  childrens: ISelectPermission[];
}
export const usePermissionStore = defineStore({
  id: 'permissionModule',
  state: () => ({
    permission: <ISelectPermission[]>[],
    selectPermission: <ISelectPermission[]>[],
    buttonRulesList: <ISelectPermission[]>[],
    buttonRulesObj: <any>{},
    activeMenu: sessionStorage.getItem("activeMenu") || "",
  }),
  actions: {
    //保存选中菜单
    setActiveMenu(data: any) {
      this.$state.activeMenu = data;
      sessionStorage.setItem("activeMenu", data);
    },
    setPermission(data: any) {
      this.$state.permission = data;
    },
    //设置选中的菜单权限
    setSelectPermission(index: number) {
      let selectPermission = [] as ISelectPermission[];
      //默认1级是平台，2级是头部菜单
      this.$state.permission[0].childrens.forEach(
        (element: ISelectPermission, idx: number) => {
          if (idx === index) {
            selectPermission = element.childrens;
          }
        }
      );
      this.$state.selectPermission = selectPermission;
    },
    setButtonRulesList(data: any) {
      this.$state.buttonRulesList = data;
    }
  }
});
